package codeCreate.strategyService.impl;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.ObjectUtils.Null;

import codeCreate.BaseConfiguration;
import codeCreate.domain.User;
import codeCreate.strategyService.StrategyService;

public class ServiceStrategy extends BaseConfiguration implements StrategyService<String> {
    private String SERVICE_PATH = "/src/codeCreate/service/";
    private String SERVICEIMPL_PATH = "/src/codeCreate/service/impl/";
    private String PREFIX = "I";
    private String IMPL_SUFFIX = "ServiceImpl";
    private String SUFFIX = "Service";
    private static String[] methods_user = { "String getUserName(Long id)", "User getUser(Long id)" };
    private static Map<String, String[]> map = new HashMap<>();
    static {
        map.put("User", methods_user);
    }

    @Override
    public String create() throws Exception {
        // TODO Auto-generated method stub
        Iterator<Entry<String, String[]>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, String[]> entry = iterator.next();
            String keyName = entry.getKey();
            String[] methods = entry.getValue();
            // 服务类
            StringBuffer serviceName = new StringBuffer();
            serviceName.append(PROJECT_PATH).append(SERVICE_PATH).append(PREFIX).append(keyName).append(SUFFIX)
                    .append(FILE_TYPE);
            File serviceFile = new File(serviceName.toString());
            FileWriter serviceFw = null;
            serviceFw = new FileWriter(serviceFile);
            StringBuffer serviceContext = new StringBuffer();
            // package信息
            serviceContext.append(PACKAGE).append(resolvePath(SERVICE_PATH)).append(DELIMITER).append(NL_2);
            // 类名头信息
            serviceContext.append(PUBLIC).append(INTERFACE).append(PREFIX).append(keyName).append(SUFFIX)
                    .append(BRACE_PREFIX);
            // 方法信息
            int flag = 1;
            for (String method : methods) {
                serviceContext.append(TAB).append(PUBLIC).append(method).append(THROWS).append(EXCEPTION)
                        .append(DELIMITER);
                if (flag < methods.length) {
                    serviceContext.append(NL_2);
                } else {
                    serviceContext.append(NL);
                }
                flag++;
            }
            serviceContext.append(BRACE_SUFFIX);
            serviceFw.write(serviceContext.toString());
            serviceFw.close();
            // 服务实现类
            StringBuffer implName = new StringBuffer();
            implName.append(PROJECT_PATH).append(SERVICEIMPL_PATH).append(keyName).append(IMPL_SUFFIX)
                    .append(FILE_TYPE);
            File implFile = new File(implName.toString());
            FileWriter implFw = null;
            implFw = new FileWriter(implFile);
            StringBuffer implContext = new StringBuffer();
            // package头信息
            implContext.append(PACKAGE).append(resolvePath(SERVICEIMPL_PATH)).append(DELIMITER).append(NL_2);
            // 类名头信息
            implContext.append(PUBLIC).append(CLASS).append(keyName).append(IMPL_SUFFIX).append(IMPLEMENTS)
                    .append(PREFIX).append(keyName).append(SUFFIX).append(BRACE_PREFIX);
            int flag2 = 1;
            for (String method : methods) {
                implContext.append(TAB).append(PUBLIC).append(method).append(THROWS).append(EXCEPTION)
                        .append(BRACE_PREFIX).append(TAB_2).append(DEFAULT_RETURN).append(NL);
                if (flag2 < methods.length) {
                    implContext.append(TAB).append(BRACE_SUFFIX).append(NL);
                } else {
                    implContext.append(TAB).append(BRACE_SUFFIX).append(NL);
                }
                flag2++;
            }
            implContext.append(BRACE_SUFFIX);
            implFw.write(implContext.toString());
            implFw.close();

        }
        return null;
    }

    private String resolvePath(String orginalPath) {
        orginalPath = orginalPath.substring(5, orginalPath.length() - 1);
        return orginalPath.replace("/", ".");
    }
}
